package com.hello.system.controller.app;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.system.service.IUserTemplateUsageService;
import com.hello.system.service.IStationeryTemplatesService;
import com.hello.system.domain.StationeryTemplates;
import lombok.extern.slf4j.Slf4j;

/**
 * 小程序用户模板使用记录Controller
 * 
 */
@Slf4j
@RestController
@RequestMapping("/app/template/usage")
public class AppTemplateUsageController extends BaseController
{
    @Autowired
    private IUserTemplateUsageService userTemplateUsageService;
    
    @Autowired
    private IStationeryTemplatesService stationeryTemplatesService;

    /**
     * 检查模板使用是否免费
     */
    @GetMapping("/check")
    public AjaxResult checkFreeUsage(@RequestParam("userId") Long userId, @RequestParam("templateId") Long templateId)
    {
        boolean isFree = userTemplateUsageService.isFreeTemplateUsage(userId, templateId);
        int usageCount = userTemplateUsageService.getUsageCount(userId, templateId);
        return success(new FreeUsageResult(isFree, usageCount));
    }
    
    /**
     * 记录模板使用
     */
    @PostMapping("/record")
    @Log(title = "记录模板使用", businessType = BusinessType.INSERT)
    public AjaxResult recordUsage(@RequestParam("userId") Long userId, @RequestParam("templateId") Long templateId)
    {
        return toAjax(userTemplateUsageService.recordTemplateUsage(userId, templateId));
    }
    
    /**
     * 获取用户所有模板使用情况
     */
    @GetMapping("/list")
    public AjaxResult getUserTemplateUsageList(@RequestParam("userId") Long userId)
    {
        // 获取用户所有模板使用情况
        Map<Long, Integer> usageMap = userTemplateUsageService.getUserAllTemplateUsage(userId);
        
        // 查询所有模板信息
        StationeryTemplates queryParam = new StationeryTemplates();
        queryParam.setIsActive(1); // 只查询激活的模板
        List<StationeryTemplates> allTemplates = stationeryTemplatesService.selectStationeryTemplatesList(queryParam);
        
        // 组合模板信息和使用次数
        List<Map<String, Object>> result = new ArrayList<>();
        for (StationeryTemplates template : allTemplates) {
            Map<String, Object> templateInfo = new HashMap<>();
            Long templateId = template.getTemplateId();
            
            templateInfo.put("templateId", templateId);
            templateInfo.put("name", template.getName());
            templateInfo.put("description", template.getDescription());
            templateInfo.put("category", template.getCategory());
            templateInfo.put("type", template.getType());
            templateInfo.put("previewImageUrl", template.getPreviewImageUrl());
            templateInfo.put("price", template.getPrice());
            templateInfo.put("usageCount", usageMap.getOrDefault(templateId, 0));
            templateInfo.put("isFree", usageMap.getOrDefault(templateId, 0) < 3); // 前三次免费
            
            result.add(templateInfo);
        }
        
        return success(result);
    }
    
    /**
     * 获取用户模板总使用次数
     */
    @GetMapping("/total")
    public AjaxResult getTotalUsage(@RequestParam("userId") Long userId)
    {
        int totalUsage = userTemplateUsageService.getTotalUsageCount(userId);
        Map<String, Integer> result = new HashMap<>();
        result.put("totalUsage", totalUsage);
        return success(result);
    }
    
    /**
     * 免费使用结果封装
     */
    static class FreeUsageResult {
        private boolean isFree;
        private int usageCount;
        
        public FreeUsageResult(boolean isFree, int usageCount) {
            this.isFree = isFree;
            this.usageCount = usageCount;
        }
        
        public boolean getIsFree() {
            return isFree;
        }
        
        public void setIsFree(boolean isFree) {
            this.isFree = isFree;
        }
        
        public int getUsageCount() {
            return usageCount;
        }
        
        public void setUsageCount(int usageCount) {
            this.usageCount = usageCount;
        }
    }
} 